package elitech.vietnam.myfashion.extend;

import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.config.Const;
import elitech.vietnam.myfashion.dialog.OkCancelWarnningDialog;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.entites.Member;

public class ListFollowerArrayAdapter extends ArrayAdapter<Member> {
	private MainActivity mContext;
	private ListFollowerArrayAdapter mAdapter;
	private boolean isFollowing = false;
	
	public ListFollowerArrayAdapter(MainActivity context, int resource,
			List<Member> objects, boolean isFollowing) {
		super(context, resource, objects);
		this.isFollowing = isFollowing;
		mContext = context;
		mAdapter = this;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		final ViewHolder holder;
		final Member i = getItem(position);

		if (row == null) {
			LayoutInflater inflater = LayoutInflater.from(getContext());
			row = inflater.inflate(R.layout.followlistitem, parent, false);

			holder = new ViewHolder();
			holder.mAvatar = (ImageView) row
					.findViewById(R.id.followitem_image);
			holder.mName = (TextView) row.findViewById(R.id.followitem_name);
			holder.mProfile = (TextView) row
					.findViewById(R.id.followitem_profile);
			holder.mBtnUnfollow = (Button) row
					.findViewById(R.id.followitem_btnUnfollow);
			row.setTag(holder);
		} else
			holder = (ViewHolder) row.getTag();

		UrlImageViewHelper.setUrlDrawable(holder.mAvatar, Const.SERVER_IMAGE_THUMB_URL + i.Image, R.drawable.no_avatar);
		holder.mName.setText((i.NickName.equals("")) ? i.Name : i.NickName);
		holder.mProfile.setText(i.Profile);
		if (isFollowing) {
			holder.mBtnUnfollow.setVisibility(View.VISIBLE);
			holder.mBtnUnfollow.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					new OkCancelWarnningDialog(mContext, String.format(mContext.getString(R.string.unfollowconfirm), holder.mName.getText().toString())) {
						public void onOk() {
							new CallServiceTask() {
								int rs = 0;
								public void work() {
									try {
										rs = service.unFollow(mContext.mUser.Id, i.Id);
									} catch (Exception e) {
										e.printStackTrace();
									}
								};
								protected void onPostExecute(Void result) {
									if (rs == 1) {
										mAdapter.remove(i);
										notifyDataSetChanged();
									} else {
									}
								};
							}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
						};
					}.show();
				}
			});
		}
		return row;
	}

	static class ViewHolder {
		ImageView mAvatar;
		TextView mName;
		TextView mProfile;
		Button mBtnUnfollow;
	}
}
